package com.example.a14110100420.yuejian.WActivity.club;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a14110100420.yuejian.Fragment.club.Club_Course_Fragment;
import com.example.a14110100420.yuejian.Fragment.club.Club_Intro_Fragment;
import com.example.a14110100420.yuejian.Fragment.club.Club_Video_Fragment;
import com.example.a14110100420.yuejian.R;
import com.example.a14110100420.yuejian.WAdapter.club.ClubDetailViewpageAdater;
import com.example.a14110100420.yuejian.WSerivice.ClubService;
import com.example.a14110100420.yuejian.Wbase.BaseActivity;
import com.example.a14110100420.yuejian.Wbase.Conast;
import com.example.a14110100420.yuejian.Wbean.club.ClubDetail;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ClubDetailActivity extends AppCompatActivity {
    private ImageView clubpic;
    private TextView clubname;
    private TextView clubfans;
    private TextView join_text;
    private ImageView join_pic;

    private String join_res;


    private Club_Intro_Fragment club_intro_fragment;
    private String userid = BaseActivity.getUserid();


    private String club_id;
    private String fans_type="1";
    private String SessonID = "pf2cmg5jk9p24f2mv1kjf55bo3";

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<String> titles = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_detail);
        getSupportActionBar().hide();//隐藏ActionBar

        Intent intent = getIntent();
        club_id = intent.getStringExtra("clubid");
        event();
        showActivity();

        ClubDetailViewpageAdater adater = new ClubDetailViewpageAdater(getSupportFragmentManager(),
                titles,fragments);
        viewPager.setAdapter(adater);
        tabLayout.setupWithViewPager(viewPager);
        init();
        checkJoin();
        joinClub();


    }

    private void showActivity(){
        tabLayout = (TabLayout) findViewById(R.id.tabs_club);
        viewPager = (ViewPager) findViewById(R.id.clubD_viewPager);
        titles.add("俱乐部简介");
        titles.add("精品课程");
        titles.add("相关视频");
        fragments.add(new Club_Intro_Fragment());
        fragments.add(new Club_Course_Fragment());
        fragments.add(new Club_Video_Fragment());
    }
    private void event(){
        clubfans = (TextView) findViewById(R.id.fans_num);
        clubname = (TextView) findViewById(R.id.clubD_clubname);
        clubpic = (ImageView) findViewById(R.id.clubD_clubpic);
        join_pic = (ImageView) findViewById(R.id.join_nor);
        join_text = (TextView) findViewById(R.id.if_join);
    }
    private void init(){
        Intent intent =  getIntent();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Conast.base_Url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ClubService service = retrofit.create(ClubService.class);
        Call<ClubDetail> call = service.getClubDetail(intent.getStringExtra("clubid"));
        call.enqueue(new Callback<ClubDetail>() {
            @Override
            public void onResponse(Call<ClubDetail> call, Response<ClubDetail> response) {
                ClubDetail clubDetail = response.body();
                if(clubDetail != null){
                    clubfans.setText(clubDetail.getFans_num() + "人加入");
                    clubname.setText(clubDetail.getClub_name());

                    Picasso.with(getBaseContext())
                            .load("http://172.24.10.175/workout/Uploads/"+clubDetail.getThumb())
                            .into(clubpic);
                    if (clubpic.getDrawable() == null)
                    {
                        clubpic.setImageResource(R.drawable.kobe2);
                    }
                }

            }

            @Override
            public void onFailure(Call<ClubDetail> call, Throwable t) {

            }
        });

    }
    private void checkJoin(){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Conast.base_Url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ClubService service = retrofit.create(ClubService.class);
        Call<Integer> call = service.getFansState(club_id,userid,fans_type,SessonID);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body() == 0)
                {
                    join_pic.setImageResource(R.drawable.ic_join_ed);
                    join_text.setText("已关注");
                    join_res = "yes";

                }
                else {
                    join_pic.setImageResource(R.drawable.ic_join_nor);
                    join_text.setText("关注该俱乐部");
                    join_res = "no";



                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });

    }
   private void joinClub(){

        join_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Conast.base_Url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ClubService service = retrofit.create(ClubService.class);
        Call<Integer> call = service.guanzhuClub(userid,club_id,fans_type);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if ("no".equals(join_res)){
                    Toast.makeText(ClubDetailActivity.this,"关注成功",Toast.LENGTH_SHORT).show();

                }
                refresh();

            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
            }
        });
    }
    private void refresh(){
        checkJoin();
    }
}
